package com.vimi8.ebb.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by xuybin on 2016/9/5.
 */
@Controller
public class BasicErrorControllerImpl extends BasicErrorController {

    private static final Logger logger =  LoggerFactory.getLogger("BasicErrorControllerImpl");

    @Autowired
    public BasicErrorControllerImpl(ErrorAttributes errorAttributes) {
        super(errorAttributes, new ErrorProperties());
        //以下开启,则,在URL后增加trace参数,即可看到调用跟踪信息
        //this.getErrorProperties().setIncludeStacktrace(ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM);
    }


    @RequestMapping(
            produces = {"text/html"}
    )
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        //如果是401之类的错误,则跳转到登录页
        Map<String, Object> model = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML));
        logger.warn("--------------1errorHtml-----------"+model);
        HttpStatus httpStatus=this.getStatus(request);
        switch (httpStatus){
            case UNAUTHORIZED:
            case NOT_ACCEPTABLE:
                try {
                    RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
                    redirectStrategy.sendRedirect(request, response, "/login.html");
                    return new ModelAndView("error", model);
                } catch (IOException e) {
                    break;
                }
            case NOT_FOUND:
                //跳转到404静态页面
                try {
                    RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
                    redirectStrategy.sendRedirect(request, response, "/404.html");
                    return new ModelAndView("error", model);
                } catch (IOException e) {
                    break;
                }
            default:
                break;
        }
        //其他错误,出现错误页面
        response.setStatus(this.getStatus(request).value());
        return new ModelAndView("error", model);

    }

    @RequestMapping
    @ResponseBody
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        //返回的内容和OATH兼容,含有 error 和  error_description字段
        Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL));
        logger.warn("--------------error-----------"+body);
        Map<String, Object> reslut=new LinkedHashMap<>();
        reslut.put("error","");
        reslut.put("error_description","");
        if(body.containsKey("error")){
            reslut.replace("error",body.get("error"));
        }
        if(body.containsKey("message")){
            reslut.replace("error_description",body.get("message"));
        }

        HttpStatus status = this.getStatus(request);

        return new ResponseEntity(reslut, status);
    }
}
